<?php

	require_once 'AppMessages.php';
	require_once 'ConnectDb.php';

	class BuildingService {
	
		/*singleton*/		
		static private $instance = null ;
		
		static public function getInstance(){
			if ( self::$instance == null ) 
				self::$instance = new BuildingService();
				
			return self::$instance;
		}	
		
		public function addBuildingConfig($name, $costGold, $costMana, $costGems, $prodGold, $prodMana, $nextLevel) {
			
			$connectDb = ConnectDb::getInstance()->getConnexion();
			$requete = 'INSERT INTO buildings 
						VALUES("", :name, :costGold, :costMana, :costGems, :prodGold, :prodMana, :nextLevel)';
			
			$prep = $connectDb->prepare( $requete );
			
			$prep->bindValue(':name', $name, PDO::PARAM_STR);
			$prep->bindValue(':costGold', $costGold, PDO::PARAM_STR);
			$prep->bindValue(':costMana', $costMana, PDO::PARAM_STR);
			$prep->bindValue(':costGems', $costGems, PDO::PARAM_STR);
			$prep->bindValue(':prodGold', $prodGold, PDO::PARAM_STR);
			$prep->bindValue(':prodMana', $prodMana, PDO::PARAM_STR);
			$prep->bindValue(':nextLevel', $nextLevel, PDO::PARAM_STR);
			
			$prep->execute();
			return AppMessages::BUILDING_CREATION_OK;
		}
		
		public function getBuildingConfigByName($name) {
			
			$connectDb = ConnectDb::getInstance()->getConnexion();
			$requete = 'SELECT id, name, costGold, costMana, costGems, prodGold, prodMana, nextLevel 
						FROM buildings 
						WHERE name = :name';
			$prep = $connectDb->prepare( $requete );
			
			$prep->bindValue(':name', $name, PDO::PARAM_STR);
			$prep->setFetchMode( PDO::FETCH_NAMED  );
			$prep->execute();
			
			$data = $prep->fetch();
			
			if ( !$data )
				return array( AppMessages::NO_BUILDING );
			else
				return $data;
		}
		
		
	}

?>